import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:shopping_group/base/page_constance.dart';

class RouteUtil {
  //全局变量
  static GlobalKey<NavigatorState> navigatorKey = GlobalKey();
  //栈移除上个view，打开新的view
  static void goReplaceView(String name) {
    BotToast.closeAllLoading();
    pushReplacementNamed(name);
  }

  //打开新的view
  static void goView(String name) {
    BotToast.closeAllLoading();
    pushNamed(name);
  }

  //关闭当前view
  static void closeView() {
    BotToast.closeAllLoading();
    navigatorKey.currentState.pop();
  }

  //关闭当前view,带参数返回
  static void closeParameterView(Object map) {
    BotToast.closeAllLoading();
    navigatorKey.currentState.pop(map);
  }

//回到主界面,销毁栈其他view
  // static void goMainView() {
  //   BotToast.closeAllLoading();
  //   pushNamedAndRemoveUntil(PageConstance.MAIN_PAGE);
  // }

//回到登录面,销毁栈其他view
  static void goLoginView() {
    BotToast.closeAllLoading();
    pushLoginUntil();
  }

  //一直退出直到某一个页面
  static void popAppointView(String page) {
    BotToast.closeAllLoading();
    popUntil(page);
  }

//带参数跳转界面view
  static Future goParameter(StatefulWidget statefulWidget, {String pageName}) {
    BotToast.closeAllLoading();
    return navigatorKey.currentState.push(
      MaterialPageRoute(
        builder: (context) {
          return statefulWidget;
        },
        settings: RouteSettings(name: pageName ?? ''),
      ),
    );
  }

  static void pushNamed(String pageName) {
    navigatorKey.currentState.pushNamed(pageName);
  }

  static void pushReplacementNamed(String pageName) {
    navigatorKey.currentState.pushReplacementNamed(pageName);
  }

//开启一个新的页面并且销毁之前的页面
  static void pushNamedAndRemoveUntil(String path) {
    navigatorKey.currentState.pushNamedAndRemoveUntil(
      path,
      (route) => route == null,
    );
  }

  static void pushLoginUntil() {
    navigatorKey.currentState.pushNamedAndRemoveUntil(PageConstance.LOGIN_PAGE,
        ModalRoute.withName(PageConstance.LOGIN_PAGE));
  }

  //保留主页，移除中间的页面
  static void pushAndRemoveUntil(StatefulWidget statefulWidget,
      {String page: PageConstance.MAIN_PAGE}) {
    navigatorKey.currentState.pushAndRemoveUntil(
        MaterialPageRoute(builder: (context) => statefulWidget),
        ModalRoute.withName(page));
  }

  //一直退出直到某一个页面
  static void popUntil(String page) {
    navigatorKey.currentState.popUntil(ModalRoute.withName(page));
  }
}
